<?php

namespace app\admin\controller;

use app\models\{Admin, Order as Orders, Region, Agent as Agents, Riderlist, User as Users, Payment};
use think\Db;
use think\facade\Debug;
use think\Facade\Config as Configs;
use app\facade\Redis;


class Main extends Base
{

    public function welcome()
    {

        if ($this->request->isAjax()) {
            $model = new Orders();
            $post = input();
            $where = array();
            // $where[] = ['issystem', '=', 1];
            if (!empty($post['title'])) {
                $where[] = ['id|ordersn|name|mobile', 'like', '%' . $post['title'] . '%'];
            }
            if (!empty($post['created'])) {
                $start = $post['created']['startime'] ?? date('Y-m-d H:i:s');
                $end = $post['created']['endtime'] ?? date('Y-m-d H:i:s');
                $where[] = ['created', 'between', [$start, $end]];
            }
            if (!empty($post['status'])) {
                $where[] = ['status', '=', $post['status']];
            } else {
                $where[] = ['status', 'in', [2, 3, 4]];
            }
            if (!empty($post['payment'])) {
                $where[] = ['payment', '=', $post['payment']];
            }
            if (!empty($post['agentid'])) {
                $where[] = ['agentid', '=', $post['agentid']];
            }
            if (!empty($post['riderid'])) {
                $where[] = ['orderid', '=', $post['riderid']];
            }
            if (!empty($post['porderid'])) {
                $where[] = ['porderid', '=', $post['porderid']];
            }
            if (!empty($post['street'])) {
                $where[] = ['street', '=', $post['street']];
            } elseif (!empty($post['area'])) {
                $where[] = ['area', '=', $post['area']];
            } elseif (!empty($post['city'])) {
                $where[] = ['city', '=', $post['city']];
            } elseif (!empty($post['province'])) {
                $where[] = ['province', '=', $post['province']];
            }
            if (empty($post['chaxun'])) {
                return json(['code' => 200, 'msg' => '获取成功', 'data' => []]);
            }
            $arr = [
                'count' => $model->where($where)->count(), //订单量
                'ptotal' => $model->where($where)->sum('ptotal'), //实际价格
                'total' => $model->where($where)->sum('total'), //累计价格
                'ccount' => $model->where($where)->sum('ccount'),
            ];
            $list[] = $arr;
            return json(['code' => 200, 'msg' => '获取成功', 'data' => $list]);
        } else {
            $name = 'getadminweclome';
            $data = Redis::get($name);
            $list = [];
            if (false) {
                $list = unserialize($data);
            } else {
                //昨日凌晨时间
                $ystdate = date('Y-m-d H:i:s', strtotime(date('Y-m-d', time() - 86400)));
                //今日凌晨时间
                $tstdate = date('Y-m-d H:i:s', strtotime(date('Y-m-d', time())));
                $tetdate = date('Y-m-d H:i:s');
                $data = [
                    //用户
                    'users' => [
                        'count' => (new Users())->count(), //营业额
                        'yuser' => (new Users())->whereBetweenTime('created', $ystdate, $tstdate)->count(), //周同比
                        'tuser' => (new Users())->whereBetweenTime('created', $tstdate, $tetdate)->count(), // 日销售额
                        'money' => (new Payment())->where(['type' => 4, 'status' => 2, 'refund' => 1])->sum('money'),
                        'ymoney' => (new Payment())->whereBetweenTime('created', $ystdate, $tstdate)->where(['type' => 4, 'status' => 2, 'refund' => 1])->sum('money'),
                        'tmoney' => (new Payment())->whereBetweenTime('created', $tstdate, $tetdate)->where(['type' => 4, 'status' => 2, 'refund' => 1])->sum('money'),
                        //会员充值
                        'umoney' => (new Payment())->where(['type' => 2, 'status' => 2, 'refund' => 1])->sum('money'),
                        'yumoney' => (new Payment())->whereBetweenTime('created', $ystdate, $tstdate)->where(['type' => 2, 'status' => 2, 'refund' => 1])->sum('money'),
                        'tumoney' => (new Payment())->whereBetweenTime('created', $tstdate, $tetdate)->where(['type' => 2, 'status' => 2, 'refund' => 1])->sum('money')
                    ],
                    //交易额
                    'alltotal' => [
                        'money' => (new Orders())->where(['status' => [2, 3, 4], 'issystem' => 1])->sum('total'), //营业额
                        'ytotal' => (new Orders())->whereBetweenTime('created', $ystdate, $tstdate)->where(['status' => [2, 3, 4], 'issystem' => 1])->sum('total'), //周同比
                        'ttotal' => (new Orders())->whereBetweenTime('created', $tstdate, $tetdate)->where(['status' => [2, 3, 4], 'issystem' => 1])->sum('total') // 日销售额
                    ],
                    //支付金额
                    'palltotal' => [
                        'money' => (new Orders())->where(['status' => [2, 3, 4], 'payment' => [2, 3], 'issystem' => 1])->sum('total'), //营业额
                        'money1' => (new Orders())->where(['status' => [2, 3, 4], 'payment' => [1], 'issystem' => 1])->sum('total'), //营业额
                        'ttotal' => (new Orders())->whereBetweenTime('created', $tstdate, $tetdate)->where(['status' => [2, 3, 4], 'payment' => [2, 3], 'issystem' => 1])->sum('total')
                    ],
                    //下单量
                    'ordercount' => [
                        'count' => (new Orders())->where(['status' => [2, 3, 4], 'issystem' => 1])->count(), //营业额
                        'ycount' => (new Orders())->whereBetweenTime('created', $ystdate, $tstdate)->where(['status' => [2, 3, 4], 'issystem' => 1])->count(), //周同比
                        'tcount' => (new Orders())->whereBetweenTime('created', $tstdate, $tetdate)->where(['status' => [2, 3, 4], 'issystem' => 1])->count(), // 日销售额
                        'scount' => (new Orders())->where(['status' => [2, 3, 4], 'issystem' => 1])->group('user_id')->count(), //下单用户
                        'sycount' => (new Orders())->whereBetweenTime('created', $ystdate, $tstdate)->group('user_id')->where(['status' => [2, 3, 4], 'issystem' => 1])->count(), //周同比
                        'stcount' => (new Orders())->whereBetweenTime('created', $tstdate, $tetdate)->group('user_id')->where(['status' => [2, 3, 4], 'issystem' => 1])->count() // 日销售额
                    ],
                    //待服务订单量
                    'orderd' => [
                        'count' => (new Orders())->where(['status' => [2], 'issystem' => 1])->count(), //营业额
                        'total' => (new Orders())->where(['status' => [2], 'issystem' => 1])->sum('total'), //周同比
                        'totals' => (new Orders())->where(['status' => [2], 'payment' => [2, 3, 4], 'issystem' => 1])->sum('total'), // 日销售额
                        'ttotal' => (new Orders())->whereBetweenTime('created', $tstdate, $tetdate)->where(['status' => [2], 'issystem' => 1])->sum('total') // 日销售额
                    ],
                    //待服务订单量
                    'ordering' => [
                        'count' => (new Orders())->where(['status' => [3], 'issystem' => 1])->count(), //营业额
                        'total' => (new Orders())->where(['status' => [3], 'issystem' => 1])->sum('total'), //周同比
                        'totals' => (new Orders())->where(['status' => [3], 'payment' => [2, 3, 4], 'issystem' => 1])->sum('total'), // 日销售额
                    ]
                ];
                Redis::setex($name, 3600, serialize($data));
                $list = $data;
            }
            return view(null, [
                'data' => $list,
                'province' => (new Region())->where(['level' => 1])->select(),
                'agent' => (new Agents())->field('id,nickname,accounts')->where(['status' => [1, 2]])->select(),
                'rider' => (new Riderlist())->field('id,name,mobile,cards,istdz')->select()
            ]);
        }
    }

    public function theme()
    {

        return view(null, []);
    }

    public function index()
    {
        $admin = $this->request->admin;
        // $menu = config('menu.menu');
        // $arr = [];
        // foreach ($menu as $k => $v) {
        //     if ($v['show'] == true) {
        //         foreach ($v['sub'] as $x => $y) {
        //             if (($y['show'] == true && in_array($y['uri'], $admin['role'])) || $admin['is_default'] == 1) {
        //                 $arr[$k]['name'] = $v['name'];
        //                 $arr[$k]['ico'] = $v['ico'];
        //                 $arr[$k]['sub'][$x] = $y;
        //             }
        //         }
        //     }
        // }
        return view(null, [
            'admin' => $admin
        ]);
    }


    public function miss()
    {
        return view('404');
    }

    public function miss2()
    {
        return view('500');
    }

    public function admin()
    {
        $admin = Admin::get(session('admin_id'));
        if ($this->request->isPost()) {
            if (empty($admin)) {
                return json(['code' => 1, 'msg' => '登陆已过期!']);
            }
            $data = input('post.');
            if ($data['password'] != '') {
                if ($data['password'] != $data['passworder']) {
                    return json(['code' => 1, 'msg' => '密码不一致']);
                }
                $data1['password'] = md5(md5($data['password']) . md5($admin['entry']));
            }
            $data1['nickname'] = $data['nickname'];
            if ($admin->save($data1, ['id' => $admin->id])) {
                return json(['code' => 200, 'msg' => '操作成功']);
            } else {
                return json(['code' => 1, 'msg' => '操作失败']);
            }
        } else {
            return view('admin', ['admin' => $admin]);
        }
    }
}
